<template>
  <div id="app">
    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary" @click="addNotice">添加公告</el-button>
      <el-table :data="noticeList" :border="true" stripe>
        <el-table-column type="index" :resizable="false"></el-table-column>
        <el-table-column prop="noticeTitle" label="公告标题"></el-table-column>
        <el-table-column
          prop="noticeContent"
          label="公告内容"
        ></el-table-column>
        <el-table-column prop="publisher" label="发布人"></el-table-column>
        <el-table-column
          prop="noticeTime"
          label="发布时间"
          :resizable="false"
        ></el-table-column>
        <el-table-column label="操作" :resizable="false">
          <template v-slot="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="editNotice(scope.row.noticeId)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteNotice(scope.row.noticeId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      noticeList: [],
    };
  },
  created() {
    this.getNoticeList();
  },
  methods: {
    async getNoticeList() {
      const { data: res } = await this.$http.get(
        "/common/notice/notice/getNoticeList"
      );
      if (res.code != 200) {
        return this.message.error(res.message);
      } else {
        this.noticeList = res.data;
      }
    },
    addNotice() {
      this.$router.push("/addNotice");
    },
    editNotice(id) {
      this.$router.push({ path: "/addNotice", query: { id: id } });
    },
    async deleteNotice(id) {
      const res = await this.$confirm("永久删除，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err); //点取消会报错，捕获
      if (res != "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: result } = await this.$http.delete(
        "notice/notice/removeNotice",
        { params: { id: id } }
      );
      if (result.code != 200) {
        return this.$message.error(result.message);
      }
      this.$message.success(result.message);
      this.getNoticeList();
    },
  },
};
</script>
<style lang="less" scoped>
</style>